package com.anxin.act.task.complete.impl;

import com.anxin.act.task.cmd.SequentialAndParallelCountersignAddcmd;
import com.anxin.act.task.complete.TaskComplete;
import com.anxin.act.task.dao.ActTaskDao;
import com.anxin.act.task.service.JumpTaskService;
import com.anxin.act.task.service.WfActHiCommentService;
import com.anxin.act.task.service.WfActRuTaskService;
import com.anxin.act.task.vo.TaskVo;
import com.anxin.act.utils.ActTaskUtils;
import com.anxin.act.utils.ActUtils;
import com.anxin.common.utils.StringUtils;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.impl.RuntimeServiceImpl;
import org.activiti.engine.impl.bpmn.behavior.ParallelMultiInstanceBehavior;
import org.activiti.engine.impl.interceptor.CommandExecutor;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 正常提交待办
 * @author: liuxiangyu
 * @date: 2020/4/26 16:28
 */
@Service
public class SelfBuiltCompleteImpl extends TaskComplete {

    @Autowired
    private WfActHiCommentService wfActHiCommentService;

    @Autowired
    private WfActRuTaskService wfActRuTaskService;


    /**
     * 提交流程
     * @param taskVo
     */
    public String complete(TaskVo taskVo){

        // 判断任务类型
        if ("actTask".equals(taskVo.getViewRuTask().getType())){
            return this.completeActTask(taskVo);
        }else {
            return this.completeAddTask(taskVo);
        }

    }

    /**
     * 提交加签流程
     * @param taskVo
     */
    public String completeAddTask(TaskVo taskVo){
        String taskId = taskVo.getId();

        // wfActHiCommentService.addComment(taskVo.getActId(),taskVo.getId(), "前加签:" + UserUtils.getNickNamesByUserArray(taskVo.getAddUser()));
        wfActHiCommentService.addComment(taskVo,taskVo.getId());
        // 提交任务
        wfActRuTaskService.complete(taskVo.getId(),taskVo.getOpinions());

        ActTaskUtils.claimTask(taskVo.getActId());

        return taskId;
    }

    /**
     * 提交Activity流程
     * @param taskVo
     */
    public String completeActTask(TaskVo taskVo){

        // 保存审批意见
        super.addComment(taskVo);

        // 提交任务
        super.completeTask(taskVo);

        // 签收任务
        super.actTaskService.claimTaskByProcInsId(taskVo.getActId());

        return taskVo.getId();

    }




}
